import { useCallback, useEffect, useMemo } from "react";
import { useState } from "react";
import { useRequest } from "@/.umi/plugin-request";
import { useBoolean } from "ahooks";
import usePersistFn from "@/hooks/usePersistFn";
/**
 *
 */
export default (api, options) => {
  const {
    defaultFormValues = null, //默认参数
    form = {}, //表单的form
  } = options || {};
  const [current, setCurrent] = useState(1); //当前页
  const [pageSize, setPageSize] = useState(10); //当前页数量
  const [total, setTotal] = useState(0); //总数
  const [dataSource, setDataSource] = useState([]); //表格数据
  const [flushFlag, { toggle: flushToggle }] = useBoolean(false);
  const [formValues, setFormValue] = useState(defaultFormValues);
  const params = useMemo(() => {
    return {
      ...formValues,
      current: current,
      size:pageSize,
    };
  }, [current, pageSize, formValues]);
  const { run: requestTable, loading } = useRequest(() => api(params), {
    refreshDeps: [params, flushFlag],
    onSuccess: (res) => {
      if (res instanceof Error) return;
      const { records, total, size, current } = res || {};
      setDataSource(records ?? []);
      // setCurrent(current ?? 1);
      // setPageSize(size ?? 0);
      setTotal(total ?? 0);
    },
  });
  // const searchBy = usePersistFn((p) => {
  //   setCurrent(1);
  //   setFormValue({ ...formValues, ...p});
  // });
  const searchBy = (p) => {
    setCurrent(1);
    // setFormValue({ ...formValues, ...p});
    setFormValue(p);
  };
  const refresh = () => {
    //刷新
    flushToggle();
  };
  //根据form,查询
  const submit = (e) => {
    if (e?.preventDefault) {
      e?.preventDefault();
    }
    //请求参数
    const formValues = form?.getFieldsValue?.() || {};
    searchBy(formValues);
  };
  const reset = () => {
    form?.resetFields();
    setCurrent(1);
    searchBy({});
  };

  // 表格改变时触发
  const handleTableChange = useCallback(
    (pagination) => {
      setCurrent(pagination.current || current);
      setPageSize(pagination.pageSize || pageSize || 10);
    },
    [current, pageSize]
  );
  // 作用与table footer的配置
  // useEffect(()=>{
  //   // requestTable()
  // },[params,flushFlag])

  const setting = useMemo(() => {
    const onPaginationChange = (pageNum, newPageSize) => {
      setPageSize(newPageSize);
      setCurrent(pageNum);
    };

    return {
      total,
      onPaginationChange,
      current,
      pageSize,
    };
  }, [current, pageSize, total, setPageSize, setCurrent]);

  return {
    refresh,
    searchBy,
    submit,
    reset,
    tableProps: {
      dataSource,
      loading,
      onChange: handleTableChange,
      pagination: {
        pageSize,
        current,
        total,
      },
      // 以下适用于内部表格
      // 用于自定义table footer
      setting,
    },
  };
};

// export default function useTable({api,options}){
//     const {
//     defaultFormValues = {}, //默认参数
//     form = {}, //表单的form
//   } = options||{};

//   const [current, setCurrent] = useState(1); //当前页
//   const [pageSize, setPageSize] = useState(0); //当前页数量
//   const [total, setTotal] = useState(0); //总数
//   const [dataSource, setDataSource] = useState([]); //表格数据
//   useRequest(getStudentPage,{
//     onSuccess:(res)=>{
//       console.log(res)
//     }
//   })
// }
